#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>


#define  LEFT   30000000
#define  RIGHT  30000200
#define  THRNUM  4

static int num = 0;
static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;

static void *thr_prime(void *p) {
	int _num, j, mark;
	while (num >= 0) {
		mark = 1;
			pthread_mutex_lock(&mut);
			_num = num;
			num = 0;
			pthread_mutex_unlock(&mut);
		if (_num > 0) {
			for (j = 2; j < _num/ 2; j++) {
 			  	if (_num% j == 0) {
 					mark = 0;
		 	 		break;
				}
		 	}
			if (mark) {
				printf("%d is a primer\n", _num);
			}
		}
	}
	pthread_exit(NULL);
}


int main() {
	int i = 0;
	int err;
	pthread_t tid[THRNUM];
	for (i = 0; i <THRNUM; i++) {
		err = pthread_create(tid + i, NULL, thr_prime, (void*)i);
		if (err) {
			fprintf(stderr, " pthread_create():%s", strerror(err));
			exit(1);
		}
	}
	for (i = LEFT; i < RIGHT; i++) {
		if (num == 0) {
			pthread_mutex_lock(&mut);
			num = i;
			pthread_mutex_unlock(&mut);
		}
	}
	num = -1;
	for (i = 0; i <  THRNUM; i++) {
		pthread_join(tid[i], NULL);
	}
	pthread_mutex_destroy(&mut);
	exit(0);
}
